<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/25
 * Time: 14:49
 */
class AppPackageController extends CommonController
{
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = D('AppPackage');
    }
    
    /**
     * 
    * 应用包列表
    * @author wang
    * @param 
    */
    public function appList()
    {
        $where = array();
        $app = I('app');
        if(!empty($app)) $where['package_name|package_download'] = array('like','%'.$app.'%');
        $result['rows'] = $this->model
            ->where($where)
            ->order('packageid')
            ->page($_POST['page'],$_POST['rows'])
            ->select();

        foreach ($result['rows'] as $k => &$v) {
            $v['createtime']=date("Y-m-d H:i:s",$v['createtime']);
            $v['updatetime']=date("Y-m-d H:i:s",$v['updatetime']);
        }
        $result['total'] = $this->model->count();
        $this->ajaxReturn($result);
    }

    /**
    * 增加app应用包
    * @author wang
    * @param 
    */
    public function addApp(){
        $packageid = I('packageid');
        $data = I();
        $data['admin'] = session('admin');
        $data['updatetime'] = time();
    	if($packageid){
    		if($this->model->where("packageid='$packageid'")->save($data)){
            	$this->ajaxReturn(array('success' => 1, 'msg' => '应用包修改成功','id'=>$packageid));
	        }else{
	            $this->ajaxReturn(array('success' => 0, 'msg' => '应用包修改失败'));
	        }
    	}else{
            $data['id'] = (new  \Org\Twitter\IdWork(31,31))->nextId();
            $data['createtime']  = time();
    		if($this->model->add($data)){
            	$this->ajaxReturn(array('success' => 1, 'msg' => '应用包新增成功'));
	        }else{
	            $this->ajaxReturn(array('success' => 0, 'msg' => '应用包增加失败'));
	        }
    	}
    }

    /**
    * 删除app
    * @author wang
    * @param 
    */
    public function delApp(){
        $packageid = I('packageid');
        if($this->model->where("packageid='$packageid'")->delete()){
            $result = array(
                'sta' => 1,
                'packageid'=> $packageid,
                'msg' => '删除成功'
            );
        }else{
            $result = array(
                'sta' => 0, 
                'msg' => '删除失败'
            ); 
        }
        $this->ajaxReturn($result);
    }



}













